package com.tsing.bms.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsing.bms.entity.House;
import com.tsing.bms.entity.MianTain;
import com.tsing.bms.entity.Order;
import com.tsing.bms.entity.Repairs;
import com.tsing.bms.mapper.HouseMapper;
import com.tsing.bms.mapper.MainTainMapper;
import com.tsing.bms.mapper.OrderMapper;
import com.tsing.bms.mapper.RepairsMapper;
import com.tsing.bms.service.IRepairsService;
import com.tsing.bms.vo.req.*;
import com.tsing.bms.vo.resp.RepairsRespVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author TheTsing
 * @since 2021-03-29
 */
@Service
public class RepairsServiceImpl extends ServiceImpl<RepairsMapper, Repairs> implements IRepairsService {

    @Override
    public void addRepairs(RepairsAddReqVo reqVo) {
        Repairs repairs = new Repairs();
        BeanUtils.copyProperties(reqVo, repairs);
        baseMapper.insert(repairs);
    }

    @Override
    public void deleteRepairs(IdsReqVo reqVo) {
        baseMapper.deleteBatchIds(reqVo.getIds());
    }

    @Override
    public RepairsRespVo getRepairsById(IdReqVo reqVo) {
        return baseMapper.getRepairsById(reqVo.getId());
    }

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private HouseMapper houseMapper;

    @Resource
    private MainTainMapper mainTainMapper;

    @Override
    @Transactional
    public void updateRepairs(IdAndStatusReqVo reqVo) {
        Repairs repairs = new Repairs();
        repairs.setId(reqVo.getId());
        repairs.setStatus(reqVo.getStatus());
        Repairs origin = new LambdaQueryChainWrapper<>(baseMapper).select(Repairs::getId, Repairs::getStatus, Repairs::getHouseId, Repairs::getPrice, Repairs::getMaintainId).eq(Repairs::getId, reqVo.getId()).one();
        MianTain mianTain = new LambdaQueryChainWrapper<>(mainTainMapper).select(MianTain::getId, MianTain::getName).eq(MianTain::getId, origin.getMaintainId()).one();
        if (origin.getStatus() == 2 && reqVo.getStatus() == 3) {
            Order order = new Order();
            order.setHouseId(origin.getHouseId()).setType(3).setPrice(origin.getPrice())
                    .setDetail(mianTain.getName() + "消费 " + origin.getPrice() + " 元")
                    .setPay(0);
            House house = new LambdaQueryChainWrapper<>(houseMapper).select(House::getId, House::getBalance).eq(House::getId, order.getHouseId()).one();
            if (house.getBalance().compareTo(order.getPrice()) != -1) {
                order.setPay(1);
                new LambdaUpdateChainWrapper<>(houseMapper).set(House::getBalance, house.getBalance().subtract(order.getPrice())).eq(House::getId, house.getId()).update();
            }
            orderMapper.insert(order);
        }
        baseMapper.updateById(repairs);
    }

    @Override
    public Page<RepairsRespVo> repairsPage(RepairsPageReqVo reqVo) {
        Page<Repairs> page = new Page<>(reqVo.getPageNum(), reqVo.getPageSize());
        List<RepairsRespVo> list = baseMapper.repairsPage(reqVo, page);
        Page<RepairsRespVo> repairsPage = new Page<>();
        repairsPage.setTotal(page.getTotal());//总条数
        repairsPage.setCurrent(page.getCurrent());//页码
        repairsPage.setSize(page.getSize());//当前页个数
        repairsPage.setRecords(list);//当前页数据
        return repairsPage;
    }

}
